package cn.retalsystem.service.impl;

import cn.retalsystem.bean.Admin;
import cn.retalsystem.bean.Room;
import cn.retalsystem.bean.User;
import cn.retalsystem.dao.AdminMapper;
import cn.retalsystem.dao.RentalMapper;
import cn.retalsystem.dao.RoomMapper;
import cn.retalsystem.dao.UserMapper;
import cn.retalsystem.service.RentalService;
import cn.retalsystem.vo.ZulinVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Xsan
 * @Date: 2019/11/1 15:47
 * @Version 1.0
 * @描述：
 */
@Service
public class RentalServiceImpl implements RentalService {


    @Autowired
    RentalMapper rentalMapper;//租赁数据表操作
    @Autowired
    UserMapper userMapper;//用户信息操作

    @Autowired
    AdminMapper adminMapper;//操作员信息



    @Override//查询总
    public List<ZulinVo> huoQu(Integer page, Integer limit ,String fjname,Integer fjzt) {
        PageHelper.startPage(page,limit);//通过分页插件将数据分页显示
        List<ZulinVo> selectroomzl = rentalMapper.selectroomzl(fjname,fjzt);//获取值

        for (ZulinVo zulinVo : selectroomzl) {//遍历
            Integer userid = zulinVo.getUserid();//取出userid

            User user = userMapper.selectByPrimaryKey(userid);//获取用户名称


            //格式化时间撮
            DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String format = dateFormat.format(zulinVo.getSfRQ());
            zulinVo.setDate(format);

            //获取管理员名称
            Integer czy = zulinVo.getCzy();
            Admin admin = adminMapper.selectByPrimaryKey(czy);


            //房间状态显示修改
            if(zulinVo.getZhuangtai().equals("0")){
                zulinVo.setZhuangtai("未出租");
                zulinVo.setAdname(null);
                zulinVo.setYhxinxi(null);


            }else {
                zulinVo.setZhuangtai("已出租");
                zulinVo.setAdname(admin.getName());
                zulinVo.setYhxinxi(user.getUsername());
            }

        }
        return selectroomzl;
    }

    @Override
    public void deltableinfo(Integer fjid) {
        System.out.println("di");
         rentalMapper.deleteByPrimaryKey(fjid);

    }

    @Override
    public int hcconut(String fjname,Integer fjzt) {
        
        int size = rentalMapper.selectroomzl(fjname,fjzt).size();
        return size;
    }

    @Override
    public int czZt(Integer zt) {
        if(rentalMapper.selectzt(zt)!=1){
            int i = rentalMapper.upDatezt(zt);
            return i;
        }else {

            return 3;
        }



    }

    @Override
    public int tzZt(Integer zt) {
        if(rentalMapper.selectzt(zt)==1){
            int i = rentalMapper.upDatetz(zt);
            return i;
        }else {

            return 3;
        }
    }


}
